﻿
using RpcHelper;

using RpcModel;
using SqlExecHelper;
using Wedonek.RpcStore.Service.DAL;
using Wedonek.RpcStore.Service.Interface;
using Wedonek.RpcStore.Service.Model;
namespace Wedonek.RpcStore.Service.Collect
{
        internal class ServerGroupCollect : BasicCollect<ServerGroupDAL>, IServerGroupCollect
        {
                private IServerCollect _Server => this.GetCollect<IServerCollect>();
                private IServerTypeCollect _ServerType => this.GetCollect<IServerTypeCollect>();
                public void CheckIsRepeat(string typeVal)
                {
                        if(!this.BasicDAL.CheckIsRepeat (typeVal))
                        {
                                throw new ErrorException ("rpc.server.group.type.repeat");
                        }
                }
                public long AddGroup(ServerGroupDatum add)
                {
                        this.CheckIsRepeat (add.TypeVal);
                        return this.BasicDAL.Add (add);
                }

                public void SetGroup(long id, string name)
                {
                        this.BasicDAL.Set (id, name);
                }
                public ServerGroup[] QueryGroup(string name, IBasicPage paging, out long count)
                {
                        return this.BasicDAL.Query (name, paging,out count);
                }
                public ServerGroup[] GetGroups()
                {
                        return this.BasicDAL.Gets ();
                }
                public ServerGroup GetGroup(long id)
                {
                        ServerGroup group = this.BasicDAL.Get (id);
                        if (group == null)
                        {
                                throw new ErrorException("rpc.server.group.not.find");
                        }
                        return group;
                }

                public void DropGroup(long groupId)
                {
                        if (this._Server.CheckIsExistsByGroup(groupId))
                        {
                                throw new ErrorException("rpc.server.group.noallow.drop");
                        }
                        using (ITransaction tran = new LocalTransaction())
                        {
                                this._ServerType.Clear(groupId);
                                this.BasicDAL.Drop (groupId);
                                tran.Commit();
                        }
                }

                public ServerGroup[] GetGroup(long[] ids)
                {
                        return this.BasicDAL.GetServiceGroup (ids);
                }
        }
}
